package com.cdtye.itps.cms.controller.plan;

import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.authcore.model.UserSso;
import com.cdtye.common.authcore.util.AuthHelper;
import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.itps.cms.dao.defect.vo.JcwProblemDefectInfoVo;
import com.cdtye.itps.cms.dao.dept.vo.SysDeptVo;
import com.cdtye.itps.cms.dao.plan.vo.JcjcPlanDetailVo;
import com.cdtye.itps.cms.dao.plan.vo.YearPlanManagementIndexVo;
import com.cdtye.itps.cms.dao.result.*;
import com.cdtye.itps.cms.service.dept.SysDeptService;
import com.cdtye.itps.cms.service.plan.JcjcPlanDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.cdtye.common.webcore.web.AjaxJson.success;

/**
 *  详细计划管理controller
 * @author wzw
 * @date 2021-11-23
 */

@Api(tags="详细计划管理")
@RestController
@RequestMapping("/planDetail")
public class JcjcPlanDetailController {

    @Autowired
    private JcjcPlanDetailService modelService;

    @Autowired
    private SysDeptService sysDeptService;

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/listPage")
    public AjaxJson<IPage<Map<String,Object>>> listPage(JcjcPlanDetailVo vo){
        IPage<Map<String,Object>> result = modelService.listPage(vo);
        return success(result);
    }

    @ApiOperation(value = "检测计划管理首页-年计划基本信息")
    @PostMapping(value = "/getYearPlanBasic")
    public AjaxJson<Map<String,Object>> getYearPlanBasic(@RequestBody JcjcPlanDetailVo vo){
        Map result = modelService.getYearPlanBasic(vo);
        return AjaxJson.success(result);
    }

    @ApiOperation(value = "检测计划管理首页-本年度各单位年检测计划完成率")
    @PostMapping(value = "/getYearPlanCompletionRate")
    public AjaxJson<Map<String,Object>> getYearPlanCompletionRate(@RequestBody JcjcPlanDetailVo vo) {
        List result = new ArrayList();
        List<Map<String,Object>> deptList = sysDeptService.getUnitDeptListByAuth(new SysDeptVo());
        for (int i = 0; i < deptList.size(); i++) {
            vo.setUnitDeptCode(deptList.get(i).get("deptNo").toString());
            Map<String,Object> deptResult = modelService.getYearPlanCompletionRate(vo);
            Map m = new HashMap();
            m.put("deptName",deptList.get(i).get("deptName").toString());
            m.put("deptId",deptList.get(i).get("deptId").toString());
            m.put("completeNum",deptResult.get("completeNum").toString());
            m.put("totalNum",deptResult.get("totalNum").toString());
            result.add(m);
        }
        return AjaxJson.success(result);
    }

    @ApiOperation(value = "检测计划管理首页-本年度检测计划完成率")
    @PostMapping(value = "/getPlanCompletionRate")
    public AjaxJson<Map<String,Object>> getPlanCompletionRate(@RequestBody JcjcPlanDetailVo vo) {
        Map<String,Object> result = modelService.getPlanCompletionRate(vo);
        return AjaxJson.success(result);
    }


    @ApiOperation(value = "检测计划管理首页-检测计划概况")
    @PostMapping(value = "/getYearCheckedSituation")
    public AjaxJson<Map<String,Object>> getYearCheckedSituation(@RequestBody JcjcPlanDetailVo vo) {
        Map<String,Object> result = modelService.getYearCheckedSituation(vo);
        return AjaxJson.success(result);
    }

    @ApiOperation(value = "检测计划管理首页-预警信息")
    @PostMapping(value = "/getEarlyWarning")
    public AjaxJson<Map<String,Object>> getEarlyWarning(@RequestBody Map<String,Object> map){
        YearPlanManagementIndexVo vo_1C = new YearPlanManagementIndexVo();
        vo_1C.setCType("1C");
        vo_1C.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        YearPlanManagementIndexVo vo_2C = new YearPlanManagementIndexVo();
        vo_2C.setCType("2C");
        vo_2C.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        YearPlanManagementIndexVo vo_4C = new YearPlanManagementIndexVo();
        vo_4C.setCType("4C");
        vo_4C.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        List<String> railTypeList = new ArrayList<>();
        if(null!= map.get("railTypeList")){
            railTypeList = (List<String>)map.get("railTypeList");
        }
        vo_1C.setRailTypeList(railTypeList);
        vo_2C.setRailTypeList(railTypeList);
        vo_4C.setRailTypeList(railTypeList);
        if(null != map.get("year"))
        {
            vo_1C.setYear(String.valueOf(map.get("year")));
            vo_2C.setYear(String.valueOf(map.get("year")));
            vo_4C.setYear(String.valueOf(map.get("year")));
        }else{
            vo_1C.setYear(DateUtils.format(new Date(),"yyyy"));
            vo_2C.setYear(DateUtils.format(new Date(),"yyyy"));
            vo_4C.setYear(DateUtils.format(new Date(),"yyyy"));
        }
        if(map.get("earlyWarningCycle") != null){
            if(map.get("earlyWarningCycle").toString() != ""){
                vo_1C.setEarlyWarningCycle(map.get("earlyWarningCycle").toString());
                vo_2C.setEarlyWarningCycle(map.get("earlyWarningCycle").toString());
                vo_4C.setEarlyWarningCycle(map.get("earlyWarningCycle").toString());
            }
        }
        List<Map<String,Object>> list_1C = modelService.getEarlyWarning(vo_1C);
        List<Map<String,Object>> list_2C = modelService.getEarlyWarning(vo_2C);
        List<Map<String,Object>> list_4C = modelService.getEarlyWarning(vo_4C);
        Map<String, Object> result = new HashMap<>(3);
        result.put("list_1C", list_1C);
        result.put("list_2C", list_2C);
        result.put("list_4C", list_4C);
        return AjaxJson.success(result);
    }

    @PostMapping(value = "/getPlanOverWarn")
    @ApiOperation(value = "首页-检测计划超期预警情况")
    public AjaxJson<Map<String,Object>> getPlanOverWarn(@RequestBody JcjcPlanDetailVo vo){
        return AjaxJson.success(modelService.getPlanOverWarn(vo));
    }

    @PostMapping(value = "/checkPlanCashDetail")
    @ApiOperation(value = "首页-检测计划兑现详情-周期检测执行情况")
    public AjaxJson<CheckPlanCashDetailResVo> checkPlanCashDetail(@RequestBody JcjcPlanDetailVo vo){
        return AjaxJson.success(modelService.getCheckPlanCashDetail(vo));
    }

    @PostMapping(value = "/unexecutedLineTop5")
    @ApiOperation(value = "首页-检测计划兑现详情-超期未执行线路Top5")
    public AjaxJson<List<UnexecutedLineResVo>> unexecutedLineTop5(@RequestBody JcjcPlanDetailVo reqVo){
        return AjaxJson.success(modelService.getUnexecutedLineTop5(reqVo));
    }

    @PostMapping(value = "/testDataStatistics")
    @ApiOperation(value = "首页-检测数据统计或检测数据兑现情况")
    public AjaxJson<TestPlanCompleteRateResVo> testDataStatistics(@RequestBody JcjcPlanDetailVo vo){
        return AjaxJson.success(modelService.getTestPlanCompleteRate(vo));
    }

    @PostMapping(value = "/getCycleCheckCover")
    @ApiOperation(value = "首页-周期检测覆盖")
    public AjaxJson<List<CycleCheckCoverResVo>> getCycleCheckCover(@RequestBody JcjcPlanDetailVo vo){
        return AjaxJson.success(modelService.getCycleCheckCover(vo));
    }

    @PostMapping(value = "/uncoveredAreaStation")
    @ApiOperation(value = "周期检测覆盖详情-未覆盖区站")
    public AjaxJson<List<UncoveredAreaStationLineNameReqVo>> uncoveredAreaStation(@RequestBody JcjcPlanDetailVo vo){
        return success(modelService.getUncoveredAreaStation(vo));
    }

    @PostMapping(value = "/uncoveredAreaStationDetail")
    @ApiOperation(value = "周期检测覆盖详情-未覆盖区站(线路下的未覆盖区站的详情)")
    public AjaxJson<UncoveredAreaStationDetailResVo> uncoveredAreaStationDetail(@RequestBody JcjcPlanDetailVo vo) throws Exception{
        return success(modelService.getUncoveredAreaStationDetail(vo));
    }
}
